class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        set<vector<int>> resultSet;

        int n = nums.size();
        sort(nums.begin(), nums.end());

        for (int mid = 1; mid < n - 1; ++mid)
        {
            int upper = n - 1;
            int lower = 0;
            while (upper > mid && lower < mid)
            {
                int sum = nums[lower] + nums[mid] + nums[upper];
                if (sum == 0)
                {
                    resultSet.insert({nums[lower], nums[mid], nums[upper]});
                    ++lower;
                }
                if (sum < 0)
                {
                    ++lower;
                }
                else
                {
                    --upper;
                }
            }
        }

        return {resultSet.begin(), resultSet.end()};
    }
};